<?php
/**
 * index.php     Zhuayi 腾讯微博操作类
 *
 * @copyright    (C) 2005 - 2010  Zhuayi
 * @licenes      http://www.zhuayi.net
 * @lastmodify   2010-10-27
 * @author       zhuayi
 * @QQ			 2179942
 */

class appsohu extends snoopy
{
	var $app_key;
	
	var $app_secret;
	
	var $callback;
	
	var $oauth_token='';
	
	var $oauth_token_secret='';
	
	var $oauth_version = '1.0';
	
	/* 默认参数 */
	function _default()
	{
		$array['oauth_consumer_key'] = $this->app_key;
		$array['oauth_nonce'] = $this->oauth_nonce();
		$array['oauth_signature_method'] = 'HMAC-SHA1';
		$array['oauth_timestamp'] = time();
		if (!empty($this->oauth_token))
		$array['oauth_token'] = $this->oauth_token;
		$array['oauth_version'] = $this->oauth_version;
		return $array;
	}
	
	
	
	
	/* http 操作 */
	function http($url , $array = array(), $method='GET',$token='',$file='')
	{
		
		$default = $this->_default();
		if (!empty($array))
		$array = array_merge($default,$array);
		else
		$array = $default;
		
		/* 排序数组 */
		$keys = array_keys($array);
		$values = array_values($array);
		
		uksort($array, 'strcmp');
		
		$array['oauth_signature'] = $this->singn($url,$array,$method,$token);
		
		
		
		if ($method == 'GET')
		{
			foreach ($array as $key=>$val)
			{
				$f[] = $key.'='.urlencode($val);
			}

			$url .= '?'.implode('&',$f);
			$this->fetch($url);
		}
		else
		{
			$array['oauth_signature'] = $this->urlencode_rfc3986($array['oauth_signature']);
			foreach ($array as $key=>$val)
			{
				
				$array2[$key] = urldecode(trim($val));
			}
			$array = $array2;
			unset($array2);
			
			if (!empty($file))
			{
				$formfiles[$file[0]] = ZCMS_ROOT.$file[1];
				$this->_submit_type = "multipart/form-data"; 
			}

			$this->submit($url,$array,$formfiles);
		}

		return $this->reset();
	}
	
	/*
	 * 随机串（32个字符长度）随机生成
	 *
	 */
	function oauth_nonce()
	{
		return md5(mt_rand().time());
	}
	
	/* HMAC 签名算法 */
	function singn($url,$array,$method='GET',$token)
	{
		foreach ($array as $key=>$val)
		{
			$base_string[] = urldecode($key).'%3D'.$this->urlencode_rfc3986($this->urlencode_rfc3986(trim($val)));
		}
		
		$base_string = implode('%26',$base_string);
		//$this->test($base_string);
		$base_string =  $method.'&'.rawurlencode($url).'&'.$base_string;
		
		
		return  base64_encode(hash_hmac('SHA1', $base_string, $this->app_secret.'&'.$token, true));
	}
	
	/* 获取request token */
	function request_token($oauth_callback = NULL)
	{		
		return $this->http('http://api.t.sohu.com/oauth/request_token',array('oauth_callback'=>$oauth_callback));
	}
	

	
	/* 获取access token 和 授权码*/
	function access_token($oauth_verifier)
	{
		//echo $oauth_token;
		return $this->http('https://open.t.qq.com/cgi-bin/access_token',array('oauth_verifier'=>$oauth_verifier),'GET',$this->oauth_token_secret);
	}
	
	/* 获取自己的详细资料 */
	function user_info()
	{
		return $this->http('http://open.t.qq.com/api/user/info',array('format'=>'json'),'GET',$this->oauth_token_secret);
	}
	
	/* 返回值鉴定 */
	function reset()
	{
		
		if (empty($this->results))
		{
			return array('error_code'=>'-1','error_cn'=>'未知错误');
		}
		elseif (trim($this->response_code) =='HTTP/1.1 200 OK')
		{
			return strings::json_dearray($this->results);
			
		}


	}
	
	function test($array)
	{
		
		echo '<pre>';
		echo '<br>===================================================================<br>';
		print_r($array);
		echo '<br>===================================================================<br>';
		
	}
	
	function urlencode_rfc3986($input) { 
        if (is_array($input)) { 
            return array_map(array('outh', 'urlencode_rfc3986'), $input); 
        } else if (is_scalar($input)) { 
            return str_replace( 
                '+', 
                ' ', 
                str_replace('%7E', '~', rawurlencode($input)) 
            ); 
        } else { 
            return ''; 
        } 
    } 

}





?>